package com.yc.cloud.portal.controller;

import com.yc.cloud.basic.api.CommonPage;
import com.yc.cloud.basic.api.ResponseResult;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.Question;
import com.yc.cloud.portal.dto.request.QuestionCreateParam;
import com.yc.cloud.portal.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * 答题库
 *
 * @author Yancy
 * @date 2024-01-31
 */
@RestController
@RequestMapping("/question")
@Api(value = "答题库", tags = "答题库接口")
@ApiIgnore
public class QuestionController {

    @Resource
    private QuestionService questionService;


    @GetMapping
    @ApiOperation(value = "答题库列表", notes = "支持分页查询,字段模糊匹配,排序等")
    public ResponseResult<CommonPage<Question>> list(BaseQueryParam param) {
        val result = questionService.list(param);
        return ResponseResult.success(CommonPage.restPage(result));
    }


    @GetMapping("{id}")
    @ApiOperation(value = "答题库详情", notes = "根据ID查询")
    public ResponseResult<Question> get(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = questionService.get(id);
        return ResponseResult.success(result);
    }


    @PostMapping
    @ApiOperation(value = "添加答题库", notes = "添加")
    public ResponseResult<Question> create(@Validated @RequestBody QuestionCreateParam param) {
        val result = questionService.create(param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }

//    @GetMapping("{questionNo}")
//    @ApiOperation(value = "通过题库编号查询", notes = "根据题库编号查询")
//    public ResponseResult<Question> getByQuestionNo(@PathVariable @ApiParam(value = "questionNo", name = "题库编号", required = true) Long questionNo) {
//        val result = questionService.getByQuestionNo(questionNo);
//        return ResponseResult.success(result);
//    }


    @ApiOperation(value = "删除答题库", notes = "通过ID删除")
    @DeleteMapping(value = "{id}")
    public ResponseResult<Boolean> delete(@PathVariable @ApiParam(value = "ID", required = true) Long id) {
        val result = questionService.delete(id);
        return result ? ResponseResult.success(true) : ResponseResult.failed();
    }


    @ApiOperation(value = "修改答题库", notes = "通过ID修改")
    @PutMapping(value = "{id}")
    public ResponseResult<Question> update(@PathVariable @ApiParam(value = "ID", required = true) Long id, @Validated @RequestBody Question param) {
        val result = questionService.update(id, param);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed();
    }


}
